<?php

/**
 * Created by PhpStorm.
 * User: guofeng
 * Email: 290559038@qq.com
 * Date: 2016/3/31 0031
 * Time: 下午 3:23
 */

namespace common\widgets;

use yii;
use common\helper\Dump;
use yii\base\Widget;
use yii\helpers\Html;
use common\models\CatChapter;
use common\models\Menu;

class LeftMenuWidget extends Widget {

    public $message;
    public $list = [];

    public function init() {
        parent::init();
        $this->list = Menu::getMenu();
    }

    public function getLeftMenuStr($menu, $tag = 1) {
        $str = $this->getUl($tag);
        foreach ($menu as $key => $value) {
            if (isset($value['children'])) { //有子菜单
                $str .= $this->CheckCurrentUrl($value['children']);
                $str .= '<a href="#">' . $value['name'] . '<span class="fa arrow"></span></a>';
                $str .= $this->getLeftMenuStr($value['children'], $tag + 1);
            } else {
                $str .= $this->CheckCurrentUrl($value['link']);
                $str .= '<a href="' . $value['link'] . '">' . $value['name'] . '</a>';
            }
            $str .= '</li>';
        }
        $str .= '</ul>';
        return $str;
    }

    public function CheckCurrentUrl($children) {
        $arr = [];
        $controler = Yii::$app->controller;

        $currentUrl = '/' . $controler->id . '/' . $controler->action->id . '.html'; //当前link

        $ids = Menu::getIdsByLink($currentUrl);
        if (is_array($children)) {
            foreach ($children as $key => $value) {
                if (in_array($value['parent_id'], $ids)) {
                    $liStr = '<li class="active">';
                } else {
                    $liStr = '<li>';
                }
            }
        } else {
            if ($children == $currentUrl) {
                $liStr = '<li class="active">';
            } else {
                $liStr = '<li>';
            }
        }
        return $liStr;
    }

    public function getUl($tag) {
        switch ($tag) {
            case 1:
                $str = '<ul class="nav" id="side-menu">';
                break;
            case 2:
                $str = '<ul class="nav nav-second-level">';
                break;
            case 3:
                $str = '<ul class="nav nav-third-level">';
                break;
            case 4:
                $str = '<ul class="nav nav-four-level">';
                break;
        }
        return $str;
    }

    public function run() {
        $str = $this->getLeftMenuStr($this->list);
        return Html::decode($str);
    }

    //---------------菜单-------------在用---------------------------------
    
    
    public function menu() {
        $list = Menu::getMenu();
        $controller_name = Yii::$app->controller->id;
        $action_name = Yii::$app->controller->action->id;
        $url = "/" . $controller_name . "/" . $action_name . ".html";
        $ids = Menu::getIdsByLink($url);
        $return['list'] = $list;
        $return['parents'] = $ids;
        return $return;
    }

    public function menuTree($tree,$parents) {
        $date['parents'] = $parents;
        $date['tree'] = $tree;
        return $this->render("menuTree", $date);
    }
   //--------------------菜单-----------------------------------------

}
